package fr.univ.lille1.android.tests;

import android.app.Instrumentation.ActivityMonitor;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import fr.univ.lille1.android.CommentActivity_;
import fr.univ.lille1.android.R;
import fr.univ.lille1.android.SignInActivity_;

public class SignInActivityTest extends
		ActivityInstrumentationTestCase2<SignInActivity_> {
	private SignInActivity_ mActivity;
	private TextView mTextViewWelcome;
	private EditText mEditTextEmail;
	private EditText mEditTextPassword;
	private Button mButtonTagThisSignIn;

	public SignInActivityTest() {
		super(SignInActivity_.class);
	}

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		mActivity = getActivity();
		mTextViewWelcome = (TextView) getActivity().findViewById(
				R.id.textViewWelcome);
		mEditTextEmail = (EditText) getActivity().findViewById(
				R.id.editTextEmail);
		mEditTextPassword = (EditText) getActivity().findViewById(
				R.id.editTextPassword);
		mButtonTagThisSignIn = (Button) getActivity().findViewById(
				R.id.buttonTagThisSignIn);
	}

	/**
	 * Test welcome message. This TextView must display a welcome message
	 * "Welcome to TagThis" in English. This text can be translate in another
	 * language, so we must use its text in resources.
	 */
	public void testTextViewWelcome() {
		final String expected = "Welcome to TagThis";
		final String actual = mTextViewWelcome.getText().toString();
		assertEquals(expected, actual);
	}

	/**
	 * Test wrong edit text of email. If the user doesn't enter an email
	 * address, he can't log in on tag this and the application must display an
	 * error message at the user to indicate his fault. This error can be
	 * translate in another language, so we must use its text in resources.
	 */
	public void testWrongEditTextEmail() {
		final String password = "azerty";
		mActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				mEditTextPassword.setText(password);
				mButtonTagThisSignIn.performClick();
			}
		});
		getInstrumentation().waitForIdleSync();
		final String expected = mActivity
				.getString(R.string.error_message_no_value);
		final String actual = mEditTextEmail.getError().toString();
		assertTrue(mEditTextEmail.isFocused());
		assertEquals(expected, actual);
	}

	/**
	 * Test wrong edit text of password. If the user doesn't enter a password,
	 * he can't log in on tag this and the application must display an error
	 * message at the user to indicate his fault. This error can be translate in
	 * another language, so we must use its text in resources.
	 */
	public void testWrongEditTextPassword() {
		final String email = "42@example.com";
		mActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				mEditTextEmail.setText(email);
				mButtonTagThisSignIn.performClick();
			}
		});
		getInstrumentation().waitForIdleSync();
		final String expected = mActivity
				.getString(R.string.error_message_no_value);
		final String actual = mEditTextPassword.getError().toString();
		assertTrue(mEditTextPassword.isFocused());
		assertEquals(expected, actual);
	}

	/**
	 * Test launch the next activity when the user log in. If the user log in,
	 * he is authenticated on the application and the next activity is launched.
	 * We must check if CommentActivity is launched and finish it after this
	 * assert to pass on the next test.
	 */
	public void testLaunchNextActivity() {
		final ActivityMonitor activityMonitor = getInstrumentation()
				.addMonitor(CommentActivity_.class.getName(), null, false);
		final String email = "42@example.com";
		final String password = "azerty";
		mActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				mEditTextEmail.setText(email);
				mEditTextPassword.setText(password);
				mButtonTagThisSignIn.performClick();
			}
		});
		CommentActivity_ nextActivity = (CommentActivity_) getInstrumentation()
				.waitForMonitor(activityMonitor);
		assertNotNull(nextActivity);
		nextActivity.finish();
	}
}
